package show.lmm.spring_data_r2dbc.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import show.lmm.spring_data_r2dbc.core.entity.UserInfo;
import show.lmm.spring_data_r2dbc.service.UserService;

@RestController
@RequestMapping("/user")
public class UserController {

    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @GetMapping("/add/{userName}")
    public Mono<Boolean> addUser(@PathVariable("userName") String userName) {
        return userService.insert(userName);
    }

    @GetMapping("/delete/{userId}")
    public Mono<Boolean> delete(@PathVariable("userId") int userId) {
        return userService.delete(userId);
    }

    @GetMapping("/get/{userId}")
    public Mono<UserInfo> getUserInfo(@PathVariable("userId") int userId) {
        return userService.get(userId);
    }

    @GetMapping("/get/max_user_id")
    public Mono<Integer> getMaxUserId() {
        return userService.getMaxUserId();
    }

    @GetMapping("/list/{dbName}")
    public Flux<UserInfo> list(@PathVariable("dbName") String dbName) {
        return userService.list(dbName);
    }

    @GetMapping("/test_trans/{userName}")
    public Flux<UserInfo> testTrans(@PathVariable("userName") String userName) {
        return userService.testTransaction(userName);
    }
}
